package com.example.smartcity.fragment;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.content.Context;

import androidx.annotation.NonNull;

import com.example.smartcity.R;
import com.example.smartcity.utils.APIConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class JAdviseFragment extends BaseFragment implements View.OnClickListener{

    private EditText content;
    private Button btn_send;
    private static final String TAG = JAdviseFragment.class.getSimpleName();


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==1) {
                String re = msg.obj.toString();
                content.setText("");
                Toast.makeText(getActivity(),"反馈成功："+re,Toast.LENGTH_LONG).show();
            }
        }
    };


    @Override
    public View initView() {
        Log.i(TAG,"建言献策");
        View view = View.inflate(getActivity(), R.layout.jd_advice, null);
        content = view.findViewById(R.id.content);

        btn_send = view.findViewById(R.id.btn_send);

        btn_send.setOnClickListener(this);

        return view;
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                SendAdavice();
                break;

        }
    }

    private void SendAdavice() {
        String contentString = content.getText().toString().trim();
        if (TextUtils.isEmpty(contentString)) {
            Log.i("内容","内容不能为空");
            Toast.makeText(getActivity(), "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        SharedPreferences sp = getActivity().getSharedPreferences("token_data",0);
        String username = sp.getString("username","");
        String token = sp.getString("token","");
Log.i("sharedPreferences","username="+sp.getString("username","")+",userid="+sp.getString("token",""));
        OkHttpClient client = new OkHttpClient();
        JSONObject object = new JSONObject();
        try {
            object.put("content",contentString);
            object.put("uid",token);
            object.put("username",username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType,object.toString());
        Log.i("建言献策发的东西",object.toString());
        Request request = new Request.Builder()
                .post(requestBody)
                .url(APIConfig.BASE_URL+"/sendadvice")
                .addHeader("Authorization",token)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    Message msg = new Message();
                    msg.what=1;
                    msg.obj=result;
                    handler.sendMessage(msg);
                }
            }
        });
    }
}
